<?php

namespace App\Http\Controllers;

use App\Models\Gift;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GiftController extends Controller
{
    /**
     * 获取礼品列表
     */
    public function index(Request $request)
    {
        $query = Gift::query();

        // 状态筛选
        if ($request->has('status')) {
            $query->where('status', $request->status);
        }

        // 是否需要物流筛选
        if ($request->has('need_shipping')) {
            $query->where('need_shipping', $request->need_shipping);
        }

        // 按排序权重排序
        $gifts = $query->orderBy('sort_order', 'asc')
                      ->orderBy('id', 'desc')
                      ->paginate($request->input('per_page', 15));

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gifts
        ]);
    }

    /**
     * 创建礼品
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:100',
            'description' => 'required|string',
            'points_required' => 'required|integer|min:1',
            'stock' => 'required|integer',
            'status' => 'required|in:0,1',
            'start_time' => 'required|date',
            'end_time' => 'nullable|date|after:start_time',
            'sort_order' => 'integer|min:0',
            'need_shipping' => 'boolean',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 1,
                'message' => $validator->errors()->first(),
            ], 422);
        }

        $gift = Gift::create($request->all());

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gift
        ]);
    }

    /**
     * 获取礼品详情
     */
    public function show(Gift $gift)
    {
        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gift
        ]);
    }

    /**
     * 更新礼品信息
     */
    public function update(Request $request, Gift $gift)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'string|max:100',
            'description' => 'string',
            'points_required' => 'integer|min:1',
            'stock' => 'integer',
            'status' => 'in:0,1',
            'start_time' => 'date',
            'end_time' => 'nullable|date|after:start_time',
            'sort_order' => 'integer|min:0',
            'need_shipping' => 'boolean',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 1,
                'message' => $validator->errors()->first(),
            ], 422);
        }

        $gift->update($request->all());

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gift
        ]);
    }

    /**
     * 删除礼品
     */
    public function destroy(Gift $gift)
    {
        $gift->delete();

        return response()->json([
            'code' => 200,
            'message' => 'success'
        ]);
    }

    /**
     * 更新礼品状态
     */
    public function updateStatus(Request $request, Gift $gift)
    {
        $validator = Validator::make($request->all(), [
            'status' => 'required|in:0,1',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 1,
                'message' => $validator->errors()->first(),
            ], 422);
        }

        $gift->update(['status' => $request->status]);

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gift
        ]);
    }

    /**
     * 更新礼品库存
     */
    public function updateStock(Request $request, Gift $gift)
    {
        $validator = Validator::make($request->all(), [
            'stock' => 'required|integer',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'code' => 1,
                'message' => $validator->errors()->first(),
            ], 422);
        }

        $gift->update(['stock' => $request->stock]);

        return response()->json([
            'code' => 200,
            'message' => 'success',
            'data' => $gift
        ]);
    }
} 